package com.doit.day06

import javafx.scene.effect.Bloom

object _03_模式匹配 {
  def main(args: Array[String]): Unit = {

    //值的匹配
    val name = "zss"

    name match {
      case "lss" => println("我不是lss，我是zss")
      case "ww" => println("我说了，我是zss")
      case "zl" => println("滚")
      case "zss" => println("终于才对了")
      case _ => println("啥也不是，这都猜不到")
    }


    val i: Int = 10

    val res: Int = i match {
      case 1 => 10
      case 2 => 11
      case 3 => 14
      case 10 => 19
      case _ => 100
    }

    println(res)

    //模式守卫
    val num = 10
    num match {
      case a if a > 10 => println(a + 103)
      case a if a > 20 => println(a + 102)
      case a if a > 5 => println(a + 101)
      case _ => println("啥也不是")
    }

    //匹配类型
    val arr: Array[Any] = Array(1, 1.2, "abc", 'c', true, Array(1, 2, 3), Array("1", "2", "3"), List(1, 2, 3), List("1", "2", "3"))
    for (elem <- arr) {
      elem match {
        case a: Int => println("我是int类型：" + a)
        case a: Double => println("我是double类型：" + a)
        case a: String => println("我是String类型：" + a)
        case a: Char => println("我是char类型：" + a)
        case a: Boolean => println("我是Boolean类型：" + a)
        case a: Array[Int] => println("我是ARRAY的Int类型：" + a)
        case a: Array[String] => println("我是ARRAY的String类型：" + a)
        //在模式匹配中，list匹配类型的时候，只能够匹配List 但是不能够匹配集合中元素的类型
        //泛型的擦除
        case a: List[Int] => println("我是List的Int类型：" + a)
        case a: List[String] => println("我是List的String类型：" + a)
        case _ => println("啥也不是")
      }
    }


    //匹配数组
    val arr1 = Array(Array(1, 2, 3), Array("1", "2", 3), Array(1, 2), Array(1, 2, 3, 4))
    for (elem <- arr1) {
      elem match {
        case Array(1) => println("数组里面只有一个元素，并且这一个元素必须是int类型的1")
        case Array(1, _) => println("数组里面有两个元素，并且这第一个元素必须是int类型的1,第二个元素是什么我不关心")
        case Array(1, _, 3) => println("数组里面有三个元素，并且这第一个元素必须是int类型的1，第三个是3,第二个元素是什么我不关心")
        case Array("1", _*) => println("数组里面至少有一个元素，第一个必须是字符串的1")
        case Array(_, _, _) => println("数组里面有三个元素，值是什么无所谓")
        case _ => println("这是一个兜底的")
      }
    }

    //匹配元祖
    val tps: Array[Product] = Array((1, 2), (1, 2, 3), (1, "2", "3"), (1, 2, 3, 4))
    for (elem <- tps) {
      elem match {
        case (_, 1) => println("二元元祖，第二个是1")
        case (_, _) => println("二元元祖")
        case (1, 2, _) => println("三元元祖，第一个，第二个分别是1 2 ")
        case (1, _, _, _) => println("四元元祖，第一个是1")
        case _ => println("不关心")
      }
    }

    //匹配对象(解构匹配)
    val lion: Lion = Lion(1, "辛巴", 2)
    lion match {
      case Lion(1,"zss",2) =>
      case Lion(2,"lss",_) =>
      case Lion(3,"ww",2) =>
      case Lion(4,"zl",2) =>
      case Lion(5,"hehe",2) =>
      case _ =>
    }

    val lion1: Lion1 = new Lion1(1, "辛巴", 2)
    lion1 match {
      case Lion1(1,"zss",2) =>
      case Lion1(2,"lss",_) =>
      case Lion1(3,"ww",2) =>
      case Lion1(1,"辛巴",2) => println("heh")
      case Lion1(5,"hehe",2) =>
      case _ =>
    }


    //解构匹配的变形，额外的扩展用的还挺多的
    val id = 1
    val name1 = "zss"

//    val (id1,name2) = (1,"zss")

    val map: Map[Int, String] = Map((1, "zss"), (2, "lss"), (3, "ww"))
    for (elem <- map) {
      val key: Int = elem._1
      val value: String = elem._2
    }

    for((_,_) <- map){
      //这样写的目的是  就是为了单纯的循环多少次
    }

    val ints: Array[Int] = Array(1, 2, 3, 4, 5)
    for ( _ <- ints) {
      //干我具体的逻辑
    }




  }

}

case class Lion(id:Int,name:String,age:Int)

class Lion1(val id:Int,val name:String,val age:Int)
/*{
  def apply(id: Int, name: String, age: Int): Lion1 = new Lion1(id, name, age)

  def unapply(arg: Lion1): Option[(Int, String, Int)] = Some(arg.id, arg.name, arg.age)
}*/

object Lion1{
//  def apply(): Lion1 = new Lion1()
  def unapply(arg: Lion1): Option[(Int, String, Int)] = Some(arg.id,arg.name,arg.age)
}
